﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using System.Web;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{

	//分析manifest.xsf中xsf:submit的内容.
	class XSNManifestSubmitParseWorker
	{
		//CID 0263 //2009.03.31 //Begin
		public static void parseSubmitRuleSet( HttpContext context, Manifest manifest )
		{
			if( manifest.Submit != null && manifest.Submit.RuleSetName != null )
			{
				manifest.Submit.RuleSet = XSNManifestRuleSetParseWorker.parseRuleSetByName( manifest, manifest.Submit.RuleSetName );
			}
		}
		//CID 0263 //2009.03.31 //End

		public static void parseSubmit( HttpContext context, Manifest manifest ){
			XmlElement root = manifest.Doc.DocumentElement;
			string xsfPrefix = manifest.XsfPrefix;

			string submitFilter = NamespaceUtil.toFullName(xsfPrefix, "submit");

			XmlElement submitElement = (XmlElement)DOMUtil.selectANode( root, submitFilter, manifest.NsResolver );
			if( submitElement != null ){
				Submit submit = new Submit();
				//使用Ado,WS,WebDav,Email,UseQuery,SubmitToHost等方式提交,这些都是DataAdapter.
				Object dataAdapter = XSNManifestDataAdapterParseWorker.parseDataAdapterInContainer( context, manifest, submitElement );
				if( dataAdapter != null )
				{
					if( dataAdapter is DataADOAdapter )
					{
						submit.AdoAdapter = (DataADOAdapter)dataAdapter;
					}
					if( dataAdapter is DataWSAdapter )
					{
						submit.WSAdapter = (DataWSAdapter)dataAdapter;
					}
					if( dataAdapter is DataDavAdapter )
					{
						submit.DavAdapter = (DataDavAdapter) dataAdapter;
					}
					if( dataAdapter is DataEmailAdapter )
					{
						submit.EmailAdapter = (DataEmailAdapter) dataAdapter;
					}
					if( dataAdapter is SubmitToHostAdapter )
					{
						submit.SubmitToHostAdapter = (SubmitToHostAdapter)dataAdapter;
					}
					if( dataAdapter is DataUseQueryAdapter )
					{
						if( manifest.Query != null )
                        {
                            //CID 1109 //2012.02.23 //Begin
							if( manifest.Query.AdoAdapter != null )
							{
                                submit.AdoAdapter = manifest.Query.AdoAdapter;
								manifest.IsADOSubmit = true;
							}
                            else if (manifest.Query.SPListAdapter != null)
                            {
                                submit.SPListAdapter = manifest.Query.SPListAdapter;
                            }
                            //CID 1109 //2012.02.23 //End
						}
					}
				}
				else
				{
					//useScriptHandler和useHttpHandler是不包含在DataAdapters中的.
					//判断是否是使用Script脚本提交.
					string useScriptHandlerFilter = NamespaceUtil.toFullName(xsfPrefix, "useScriptHandler");
					XmlElement useScriptHandlerElement = (XmlElement)DOMUtil.selectANode( submitElement, useScriptHandlerFilter, manifest.NsResolver );
					if( useScriptHandlerElement != null )
					{
						submit.UseScriptHandler = true;
					}

					//是否提交到Web Server.
					//useHttpHandler不是一个Handler,但是为了兼容DataWebAdapter的处理,仍然构造成一个DataHttpAdapter.
					string useHttpHandlerFilter = NamespaceUtil.toFullName(xsfPrefix, "useHttpHandler");
					XmlElement useHttpHandlerElement = (XmlElement)DOMUtil.selectANode( submitElement, useHttpHandlerFilter, manifest.NsResolver );
					if( useHttpHandlerElement != null )
					{
						string href = useHttpHandlerElement.GetAttribute( "href" );
						string method = useHttpHandlerElement.GetAttribute( "method" );
						DataHttpAdapter httpAdapter = new DataHttpAdapter();
						httpAdapter.Name = href;
						httpAdapter.Href = href;
						httpAdapter.Method = method;
						submit.HttpAdapter = httpAdapter;
					}
					//CID 0263 //2009.03.31 //Begin
					string ruleSetActionFilter = NamespaceUtil.toFullName(xsfPrefix, "ruleSetAction");
					XmlElement ruleSetActionElement = (XmlElement)DOMUtil.selectANode( submitElement, ruleSetActionFilter, manifest.NsResolver );
					if( ruleSetActionElement != null )
					{
						submit.RuleSetName = ruleSetActionElement.GetAttribute( "ruleSet" );
					}
					//CID 0263 //2009.03.31 //End
				}

				string onAfterSubmit = submitElement.GetAttribute( "onAfterSubmit" );
				if( onAfterSubmit == null || onAfterSubmit.Length <= 0 )
				{
					submit.OnAfterSubmit = Submit.AfterSubmit_keepOpen;
				}
				else
				{
					submit.OnAfterSubmit = onAfterSubmit;
				}

				string showStatusDialog = submitElement.GetAttribute( "showStatusDialog" );
				if( showStatusDialog != null && showStatusDialog.Equals( "no" ) )
				{
					submit.ShowStatusDialog = false;
				}
				else
				{
					submit.ShowStatusDialog = true;
				}

				XmlElement successMessageElement = (XmlElement)(XmlElement)DOMUtil.selectANode( submitElement, NamespaceUtil.toFullName(xsfPrefix, "successMessage"), manifest.NsResolver );
				if( successMessageElement != null )
				{
					submit.SuccessMessage = successMessageElement.InnerText;
				}

				XmlElement errorMessageElement = (XmlElement)(XmlElement)DOMUtil.selectANode( submitElement, NamespaceUtil.toFullName(xsfPrefix, "errorMessage"), manifest.NsResolver );
				if( errorMessageElement != null )
				{
					submit.ErrorMessage = errorMessageElement.InnerText;
				}

				manifest.Submit= submit;

				parseUseDataSet( manifest );
			}
		}

		//判断WebService参数中是否包含DataSet,因为如果包含DataSet需要进行特殊处理.
		private static void parseUseDataSet( Manifest manifest )
		{
			if( manifest.Query != null && manifest.Submit != null )
			{
				if( manifest.Query.WSAdapter != null && manifest.Query.WSAdapter.UseDataSet && manifest.Submit.WSAdapter != null )
				{
					DataWSAdapter wsAdapter = manifest.Submit.WSAdapter;
					if( wsAdapter.Input != null && wsAdapter.Input.PartFragmentList != null && wsAdapter.Input.PartFragmentList.Count > 0 )
					{
						DataPartFragment partFragment = (DataPartFragment)wsAdapter.Input.PartFragmentList[ 0 ];
						if( partFragment.ReplaceWith != null )
						{
							int index = partFragment.ReplaceWith.LastIndexOf( "/" );
							if( partFragment.ReplaceWith.Length > index + 1 )
							{
								string lastNodeName = partFragment.ReplaceWith.Substring( index + 1 );
								string prefix = DOMUtil.getNodeNamePrefix( lastNodeName );
								string uri = null;
								if( prefix.Length <= 0 )
								{
									uri = "";
								}
								else
								{
									uri = manifest.Doc.DocumentElement.GetNamespaceOfPrefix( "" );
									if( uri == null )
									{
										uri = "";
									}
								}
								foreach( SchemaDef schema in manifest.SchemaDefList )
								{
									if( schema.Uri.Equals( uri ) )
									{
										foreach( CABFile cabFile in manifest.CabFileList )
										{
											if( cabFile.Name.ToLower().Equals( schema.Location ) )
											{
												XmlDocument schemaDoc = new XmlDocument();
												schemaDoc.LoadXml( System.Text.UTF8Encoding.UTF8.GetString( cabFile.ByteOutput ) );
												manifest.Submit.WSAdapter.SubmitSchemaDoc = schemaDoc;
											
												manifest.Submit.WSAdapter.UseDataSet = true;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}

	}
}